import 'package:get/get.dart';
import 'package:getx_simple_code/common/apis/DishesAPI.dart';
import 'package:getx_simple_code/pages/diovc/pojo/dishesEntity.dart';
import 'package:getx_simple_code/pages/diovc/pojo/dishesParam.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'state.dart';

class RefreshLogic extends GetxController {

  final RefreshState state = RefreshState();

  /// 成员变量
  int curPage = 1;
  int pageSize = 20;
  int total = 20;
  /// UI 组件
  final RefreshController refreshController = RefreshController(
    initialRefresh: true,
  );

  /// 事件

  void onRefresh() {
    getPageList(isRefresh: true).then((_) {
      refreshController.refreshCompleted(resetFooterState: true);
    }).catchError((_) {
      refreshController.refreshFailed();
    });
  }

  void onLoading() {
    if (state.dataList.length < total) {
      getPageList().then((_) {
        refreshController.loadComplete();
      }).catchError((_) {
        refreshController.loadFailed();
      });
    } else {
      refreshController.loadNoData();
    }
  }
  Future<void> getPageList({bool isRefresh = false}) async {
    if (isRefresh == true) {
      curPage = 1;
      total = 20;
      state.dataList=[];
    } else {
      curPage++;
    }
    DishesParam params = DishesParam(page: curPage,pageSize: pageSize) ;
    var result = await DishesAPIPage.dishesPageList(param: params);
    state.dataList.addAll(result.data?.records as List<Records>);
    total = result.data?.total as int;
    print('数据个数=${state.dataList.length}');
    update();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }
  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }
}
